/**
 * <p>Title: ServiceZMCL06.java</p>
 * <p>Description: </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 高强
 * @version 1.0
 * <pre>Histroy:
 *          2011-6-28 下午05:47:25  高强  Create
 *</pre>
 */
package com.baosight.sczx.zm.cl.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiBlock;
import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.common.zm.cInterface.cl.SecurityItems;
import com.baosight.sczx.zm.cl.domain.ZMCLItemAssociation;

/**
 * <p>Title: 新增检查项目</p>
 * <p>Description: </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 高强
 * @version 1.0
 * <pre>Histroy:
 *          2011-4-6 高强  Create
 *</pre>
 */
public class ServiceZMCL93 extends ServiceEPBase {
	private String fdParentId = "";
	private String fdSecuritypathId = "";
	private  static List<String> msgList ;
	
	/**新增安全检查项目 进入初始化
	 * <p>Function: initLoad</p>
	 * <p>Description: </p>
	 * <p>return value: EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-4-6 高强  Create
	 * </p>
	 *
	*/
	@Override
	public EiInfo initLoad(EiInfo inInfo) {
		EiInfo outInfo = this.query(inInfo);
		return outInfo;

	}
	
	/** 查询目前巡检点下的所有安全检查项目
	 * <p>Function: queryItem</p>
	 * <p>Description: </p>
	 * <p>return value: EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-4-6 高强  
	 * </p>
	 *
	*/
	public EiInfo query(EiInfo inInfo) {
		fdParentId = inInfo.getString("fdParentId");
		fdSecuritypathId = inInfo.getString("fdSecuritypathId");
		StaticTools.setQueryParameters(inInfo, "fdParentId", fdParentId, true);//添加查询条件
		StaticTools.setQueryParameters(inInfo, "fdSecuritypathId", fdSecuritypathId, true);//添加查询条件
		StaticTools.setQueryParameters(inInfo, "fdStatus", "在用", true);//添加查询条件
		inInfo.addBlock(EiConstant.resultBlock).set(EiConstant.offsetStr, 0);
		inInfo.getBlock(EiConstant.resultBlock).set(EiConstant.limitStr, 1000);
		inInfo.getBlock(EiConstant.resultBlock).set(EiConstant.isCountFlag, true);
		EiInfo outInfo = super.query(inInfo, "ZMCLItemAssociation.query", new ZMCLItemAssociation());
		return outInfo;
	}
	
	/** 查询所有的安全检查项目
	 * <p>Function: query</p>
	 * <p>Description: </p>
	 * <p>return value: EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-4-6 高强  
	 * </p>
	 *
	*/
	public EiInfo queryAllItem(EiInfo inInfo) {
		EiInfo outInfo = getItemInfo(SecurityItems.queryItem(inInfo));
		return outInfo;
	}
	
	/**
	 * 获得安全检查项目信息
	 * <p>Function: getItemInfo</p>
	 * <p>Description: </p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *   2011-4-6 高强  
	 * </p>
	 *
	 */
	@SuppressWarnings("unchecked")
	private EiInfo getItemInfo(EiInfo info)
	{
		EiBlock resultBlock = info.getBlock(EiConstant.resultBlock);
		for(int i=0; i<resultBlock.getRowCount(); i++)
		{
			String itemInfo = resultBlock.getRow(i).get("fdItemContent").toString()+"("+resultBlock.getRow(i).get("fdItemCode").toString()+")";
			resultBlock.getRow(i).put("fdItemInfo", itemInfo);
		}
		
		return info;
	}
	
	/**
	 * 检查是否添加重复的安全检查项目存在
	 * <p>Function: validate</p>
	 * <p>Description: </p>
	 * <p>return value:void</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-4-6 高强  
	 * </p>
	 *
	 */
	@SuppressWarnings("rawtypes")
	private boolean validate(EiInfo inInfo, Map<String, String> params) {
		if(params == null) {
			params = new HashMap<String, String>();
		}
		List result = dao.query("ZMCLItemAssociation.query", params);
		if(result!=null && result.size() > 0) {
			ZMCLItemAssociation data = (ZMCLItemAssociation)result.get(0);
			if(msgList == null) {
				msgList =  new ArrayList<String>();
			}
			msgList.add(data.getFdItemCode());//获得重复了的安全检查编号
			return false;
		}
		return true;
	}

	/** 新增安全检查项目
	 * <p>Function: insert</p>
	 * <p>Description: </p>
	 * <p>return value: EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-4-6 高强  
	 * </p>
	 *
	*/
	@Override
	public EiInfo insert(EiInfo inInfo) {
		EiBlock idBlock = inInfo.getBlock("myInsert");
		boolean validateValue = true;
		for(int i=0;i<idBlock.getRowCount();i++)
		{
			idBlock.setCell(i, "fdKeyId", TablePk.getInstance().getNextValue( "T_CL_ITEMASSOCIATION" ));
			idBlock.setCell(i, "fdSurveypointId", fdParentId);
			idBlock.setCell(i, "fdSecuritypathId", fdSecuritypathId);
			idBlock.setCell(i, "fdIsImportant", "否");
			idBlock.setCell(i, "fdStatus", "保存");
			Map<String, String> params = new HashMap<String, String>();
			params.put("fdSurveypointId", fdParentId);
			params.put("fdItemsId", idBlock.getCell(i, "fdItemsId").toString());
			boolean validate= validate(inInfo,params);
			validateValue= validateValue && validate;
		}
		if(!validateValue) {
			inInfo.setStatus(-1);
			StringBuffer msg = new StringBuffer();
			msg.append("选中的检查项目");
			for(int j=0;j<msgList.size();j++ )
			{
				if(j==msgList.size()-1)
				{
					msg.append(msgList.get(j));
					break;
				}
				msg.append(msgList.get(j)+"、");
				
			}
			msg.append("已经添加，请重新选择！");
			inInfo.setMsg(msg.toString());
			msgList.clear();
			return inInfo;
		}
		EiInfo outInfo = super.update(inInfo,"ZMCLItemAssociation.insert", "myInsert");
		return outInfo;
	}
	
	/**
	 * 删除 巡检点下的安全检查项目
	 * <p>Function: delete</p>
	 * <p>Description: </p>
	 * <p>return value: EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-4-6 高强  
	 * </p>
	 *
	*/
	@Override
	public EiInfo delete(EiInfo inInfo)
	{
		EiInfo _temp = super.delete(inInfo, "ZMCLItemAssociation.delete");
		return this.query(_temp);
	}
	
	/**
	 * 设置某条数据为非重点
	 * <p>Function: modifyImportantIsNo</p>
	 * <p>Description: </p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-4-6 高强  
	 * </p>
	 *
	 */
	public EiInfo modifyImportantIsNo(EiInfo inInfo)
	{
		EiInfo _temp = super.update(inInfo, "ZMCLItemAssociation.modifyImportantIsNo");
		return this.query(_temp);
	}
	
	/**
	 * 设置某条数据为重点
	 * <p>Function: modifyImportantIsYes</p>
	 * <p>Description: </p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-4-6 高强  
	 * </p>
	 *
	 */
	public EiInfo modifyImportantIsYes(EiInfo inInfo)
	{
		EiInfo _temp = super.update(inInfo, "ZMCLItemAssociation.modifyImportantIsYes");
		return this.query(_temp);
	}
}
